package com.softstao.guoyu.ui.fragment;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.flyco.tablayout.SegmentTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.softstao.guoyu.R;
import com.softstao.guoyu.base.BaseFragment;
import com.softstao.guoyu.global.SharePreferenceManager;
import com.softstao.guoyu.global.UserManager;
import com.softstao.guoyu.model.me.Balance;
import com.softstao.guoyu.model.me.BalanceIndex;
import com.softstao.guoyu.model.me.Record;
import com.softstao.guoyu.mvp.interactor.me.BalanceInteractor;
import com.softstao.guoyu.mvp.presenter.me.BalancePresenter;
import com.softstao.guoyu.mvp.viewer.me.BalanceIndexViewer;
import com.softstao.guoyu.mvp.viewer.me.RechargeListViewer;
import com.softstao.guoyu.ui.activity.me.RechargeApplyListActivity;
import com.softstao.guoyu.ui.baseAdapter.RecycleViewBaseAdapter;
import com.softstao.guoyu.ui.baseAdapter.RecycleViewHolder;
import com.softstao.softstaolibrary.library.utils.LZUtils;
import com.softstao.softstaolibrary.library.widget.CustomScrollerView;
import com.softstao.softstaolibrary.library.widget.EmptyLayout;
import com.softstao.softstaolibrary.library.widget.FullyLinearLayoutManager;
import com.wangjie.androidinject.annotation.annotations.mvp.AIPresenter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by vaikus on 2017/6/15.
 */

public class RechargeListFragment extends BaseFragment implements RechargeListViewer, BalanceIndexViewer {

    @AIPresenter(presenter = BalancePresenter.class, interactor = BalanceInteractor.class)
    BalancePresenter presenter;

    @BindView(R.id.balance)
    TextView balanceView;
    @BindView(R.id.tab_layout)
    SegmentTabLayout tabLayout;
    @BindView(R.id.empty_layout)
    EmptyLayout emptyLayout;
    @BindView(R.id.more)
    TextView more;
    @BindView(R.id.scrollView)
    CustomScrollerView scrollView;
    @BindView(R.id.list_view)
    RecyclerView listView;
    @BindView(R.id.amount_recharge)
    TextView amountRecharge;
    @BindView(R.id.amount_pay)
    TextView amountPay;
    @BindView(R.id.submit_btn)
    TextView submitBtn;
    private String[] mTitles = new String[]{"全部", "支付", "充值"};
    private List<Record> recordList = new ArrayList<>();
    private RecycleViewBaseAdapter<Record> adapter;
    private int type = -1;
    private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public int _ContentView() {
        return R.layout.fragment_recharge_list;
    }

    @Override
    public void initView() {
        setPtrFrameLayoutEnable();
        scrollView.setOnScrollChangedListener(this);
        if (UserManager.getInstance().getUser().getLevel()==5){
            submitBtn.setText("新增充值");
        }else {
            submitBtn.setText("联系上级");
        }

        tabLayout.setTabData(mTitles);
        tabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                tabLayout.setCurrentTab(position);
                switch (mTitles[position]) {
                    case "全部":
                        type = -1;
                        break;
                    case "支付":
                        type = 1;
                        break;
                    case "充值":
                        type = 0;
                        break;
                }
                currentPage = 1;
                getRechargeList();
            }

            @Override
            public void onTabReselect(int position) {

            }
        });

        adapter = new RecycleViewBaseAdapter<Record>(recordList, R.layout.recharge_list_item) {
            @Override
            public void convert(RecycleViewHolder holder, Record record) {
                RecyclerView.LayoutParams layoutParams = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                holder.itemView.setLayoutParams(layoutParams);
                holder.setText(R.id.content, record.getExplain())
                        .setText(R.id.time,format.format(new Date(record.getDate())));
                switch (record.getInorout()) {
                    case 0:
                        holder.setText(R.id.money, "+" + LZUtils.priceFormat(record.getMoney()) + "元");
                        break;
                    case 1:
                        holder.setText(R.id.money, "-" + LZUtils.priceFormat(record.getMoney()) + "元");
                        break;
                }
            }
        };
        listView.setAdapter(adapter);
        listView.setLayoutManager(new FullyLinearLayoutManager(mContext));
    }

    @Override
    public void onResume() {
        super.onResume();
        getRechargeList();
        getBalance();
    }

    @Override
    public void onRefresh() {
        super.onRefresh();
        getRechargeList();
        getBalance();
    }

    @Override
    public void onLoad() {
        super.onLoad();
        getRechargeList();
    }

    @Override
    public void getRechargeList() {
        presenter.getRechargeList(currentPage,SharePreferenceManager.getInstance().getAgentId(),type);
    }

    @Override
    public void getResult(Balance balance) {
        if (balance != null) {
            if (balance.getRecordList() != null && balance.getRecordList().size() != 0) {
                emptyLayout.setVisibility(View.GONE);
                more.setVisibility(View.GONE);
                if (currentPage==1) {
                    recordList.clear();
                }
                recordList.addAll(balance.getRecordList());
                adapter.notifyDataSetChanged();
            } else {
                if (currentPage==1) {
                    emptyLayout.setVisibility(View.VISIBLE);
                }else {
                    more.setVisibility(View.VISIBLE);
                }
            }
        }
    }

    @Override
    public void getBalance() {
        presenter.getBalance(SharePreferenceManager.getInstance().getAgentId());
    }

    @Override
    public void getIndex(BalanceIndex balanceIndex) {
        if (balanceIndex != null) {
            balanceView.setText(LZUtils.priceFormat(balanceIndex.getBalance()));
            amountRecharge.setText("总充值：+" + LZUtils.priceFormat(balanceIndex.getAmountRecharge()) + "元");
            amountPay.setText("总支付：-" + LZUtils.priceFormat(balanceIndex.getAmountPay()) + "元");
        }
    }

    @OnClick(R.id.submit_btn)
    public void onViewClicked() {
        if (UserManager.getInstance().getUser().getLevel()==5){
            startActivity(new Intent(mContext, RechargeApplyListActivity.class));
        }else {
            Intent intent = new Intent(Intent.ACTION_DIAL);
            Uri data = Uri.parse("tel:" + UserManager.getInstance().getUser().getParentMobile());
            intent.setData(data);
            startActivity(intent);
        }
    }
}
